package com.lantosec.hmrp.modules.notify.controller;

import com.lantosec.hmrp.core.kit.Result;
import com.lantosec.hmrp.core.log.ControllerLog;
import com.lantosec.hmrp.core.log.LogType;
import com.lantosec.hmrp.core.mvc.BaseController;
import com.lantosec.hmrp.modules.monitor.entity.WarningDataEntity;
import com.lantosec.hmrp.modules.monitor.service.IMonitorWarningService;
import com.lantosec.hmrp.modules.notify.entity.NotifyRecipient;
import com.lantosec.hmrp.modules.notify.service.INotifyInfoService;
import com.lantosec.hmrp.modules.notify.vo.NotifyInfoVO;
import com.lantosec.hmrp.modules.workflow.service.IWorkflowQueryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 *
 * 通知 控制器
 *
 * @author AutoCode
 * @since 2020-05-22
 */
@Api(value = "通知控制器", tags = "通知控制器")
@RestController
@RequestMapping("/notify/info")
@Log4j2
public class NotifyInfoController extends BaseController {
    @Autowired
    private INotifyInfoService service;

    @Autowired
    private IMonitorWarningService monitorWarningService;

    @Autowired
    private IWorkflowQueryService workflowQueryService;

    @GetMapping("all")
    @ApiOperation(value = "查询全部")
    public Result<List<NotifyInfoVO>> all(){
        return Result.ok(entity2VO(service.list()));
    }

    @GetMapping("getNotifySum")
    @ApiOperation(value = "查询通知已读未读人数")
    public Result<Map<String,Object>> getNotifySum(@ApiParam(value = "通知分类ID",required = true) @RequestParam(name = "notifyId") String notifyId){
        Map<String,Object> map = service.getNotifySum(notifyId);
        return Result.ok(map);
    }

    @GetMapping("getNotRead")
    @ApiOperation(value = "查询未读通知")
    public Result<List<NotifyInfoVO>> getNotRead(@ApiParam(value = "通知分类ID") @RequestParam(name = "notifyTypeId",required = false) String notifyTypeId){
        List<NotifyInfoVO> notifyInfoVOList = service.getNotRead(notifyTypeId);
        return Result.ok(notifyInfoVOList);
    }

    @GetMapping("getRead")
    @ApiOperation(value = "查询已读通知")
    public Result<List<NotifyInfoVO>> getRead(@ApiParam(value = "通知分类ID") @RequestParam(name = "notifyTypeId",required = false) String notifyTypeId){
        List<NotifyInfoVO> notifyInfoVOList = service.getRead(notifyTypeId);
        return Result.ok(notifyInfoVOList);
    }

    @GetMapping("getPublicRead")
    @ApiOperation(value = "登录显示系统公告")
    public Result<List<NotifyInfoVO>> getPublicRead(){
        List<NotifyInfoVO> notifyInfoVOList = service.getPublicRead();
        return Result.ok(notifyInfoVOList);
    }

    @GetMapping("getPublicReadAll")
    @ApiOperation(value = "查询系统公告")
    public Result<List<NotifyInfoVO>> getPublicReadAll(){
        List<NotifyInfoVO> notifyInfoVOList = service.getPublicReadAll();
        return Result.ok(notifyInfoVOList);
    }

    @PostMapping("create")
    @ControllerLog(name = "新增系统通知", type = LogType.add)
    @ApiOperation(value = "新增系统通知")
    public Result<Void> create(@ApiParam(value = "通知VO对象", required = true) @RequestBody NotifyInfoVO vo){
        service.saveNotifyInfo(vo);
        return Result.ok();
    }

    @PutMapping("updateRead")
    @ControllerLog(name = "更新为已读", type = LogType.update)
    @ApiOperation(value = "更新为已读")
    public Result<Void> updateRead(@ApiParam(value = "通知VO对象", required = true) @RequestBody List<NotifyRecipient> notifyRecipientList){
        service.updateRead(notifyRecipientList);
        return Result.ok();
    }

    @PutMapping("updateDelete")
    @ControllerLog(name = "更新为删除", type = LogType.update)
    @ApiOperation(value = "更新为删除")
    public Result<Void> updateDelete(@ApiParam(value = "通知VO对象", required = true) @RequestBody List<NotifyRecipient> notifyRecipientList){
        service.updateDelete(notifyRecipientList);
        return Result.ok();
    }

    @PostMapping("noticeSending")
    @ControllerLog(name = "预警发送通知", type = LogType.add)
    @ApiOperation(value = "预警发送通知")
    public Result<Void> noticeSending(@ApiParam(value = "通知VO对象", required = true) @RequestBody NotifyInfoVO notifyInfoVO){
        service.noticeSending(notifyInfoVO);
        return Result.ok();
    }

    @GetMapping("noticeCount")
    @ApiOperation(value = "通知数量，包括预警，流程")
    public Result<Integer> noticeCount() {
        String userId = super.getUserId();
        int count = 0;
        // 预警数量
        List<WarningDataEntity> warnList = monitorWarningService.getWarningBacklog(userId);
        count = warnList.size();

        // 消息数量
        int notifyCount = service.countNotRead(userId,"");
        count += notifyCount;

        // 待办流程数量
        long todoCount = workflowQueryService.countTodoTask(userId);
        count += (int)todoCount;
        return Result.ok(count);
    }
}
